package com.axungu.web.controller;

import com.alibaba.fastjson.JSON;
import com.axungu.common.CryptoDESUtil;
import com.axungu.common.Result;
import com.axungu.core.enums.AuthType;
import com.axungu.core.modal.ssh.HostAuth;
import com.axungu.core.service.HostAuthService;
import com.axungu.exception.NoFoundException;
import com.axungu.exception.ServiceException;
import com.axungu.core.oauth.Permission;
import com.axungu.web.params.SaveHostAuthParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author jtoms.shen
 * @version 1.0
 * @date 2018/12/7 17:38
 */
@Slf4j
@Controller
@RequestMapping("/host/auth")
public class HostAuthController {

    @Value("${des.key}")
    private String desKey;

    @Autowired
    private HostAuthService hostAuthService;

    @Permission
    @GetMapping("/index.htm")
    public String index(ModelMap modelMap) {
        List<HostAuth> listData = hostAuthService.findAll();
        modelMap.addAttribute("listData", listData);
        return "host/auth/index";
    }

    @Permission
    @GetMapping("/edit.htm")
    public String edit(Long id, ModelMap modelMap) throws NoFoundException {
        HostAuth hostAuth = new HostAuth();
        if (id != null) {
            hostAuth = this.hostAuthService.findById(id);
        }
        if (hostAuth == null) {
            throw new NoFoundException();
        }
        modelMap.addAttribute("hostAuth", hostAuth);
        return "host/auth/edit";
    }


    @Permission
    @ResponseBody
    @PostMapping("/insertOrUpdate.ajax")
    public Result insertOrUpdate(Long id, SaveHostAuthParam param) throws ServiceException {

        HostAuth hostAuth = new HostAuth();
        if (id != null && id.compareTo(0L) > 0) {
            hostAuth.setId(id);
        }
        hostAuth.setName(param.getName());
        hostAuth.setAuthType(param.getAuthType());
        hostAuth.setUserName(param.getUserName());
        hostAuth.setRemark(param.getRemark());

        if (param.getAuthType().equals(AuthType.password)) {
            if (StringUtils.isBlank(param.getPassword())) {
                return Result.instance(Result.Code.SUCCESS, "密码不能为空");
            }
            hostAuth.setAuthText(CryptoDESUtil.encode(desKey, param.getPassword()));
        }

        if (param.getAuthType().equals(AuthType.sshkey)) {
            Map<String, String> authMap = new HashMap<>();
            authMap.put("privateKey", param.getPrivateKey());
            authMap.put("publicKey", param.getPublicKey());
            authMap.put("passphrase", param.getPassphrase());

            hostAuth.setAuthText(CryptoDESUtil.encode(desKey, JSON.toJSONString(authMap)));
        }


        this.hostAuthService.insertOrUpdate(hostAuth);
        return Result.instance(Result.Code.SUCCESS);
    }

    @Permission
    @ResponseBody
    @PostMapping("/delete.ajax")
    public Result delete(Long[] ids) throws ServiceException {
        this.hostAuthService.delete(ids);
        return Result.instance(Result.Code.SUCCESS);
    }
}
